package database.example.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;

import database.example.common.ConnectionFactory;
import database.example.common.CloserUtil;
import database.example.dao.DepartmentDao;
import database.example.dao.InterfaceDao;


public abstract class AbsService<T> {
	protected InterfaceDao<T> dao;//
	protected Scanner sc;
	protected Connection connection;
	
	public AbsService(Scanner sc, Connection connection) {
		this.sc = sc;
		this.connection = connection;			
	}
	
	public void prnMenu() {
		int res = 0;
		T item = null;//
		do {
			System.out.println("1. 추가 2. 삭제 3. 수정 4. 조회 5. 목록 6. 종료");//
			res = sc.nextInt();//
			try {
				switch (res) {
					case 1:
						item = inputItemInfo(true);//
						dao.insertItem(item);
						break;
					case 2:
						dao.prnItemList(null);
						System.out.println("삭제할 아이템 선택");
						item = inputItemInfo(false);//
						dao.deleteItembyNo(item);
						break;
					case 3:
						dao.prnItemList(null);
						System.out.println("수정할 아이템 선택");
						item = inputItemInfo(true);//
						dao.updateItembyNo(item);
						break;
					case 4:
						dao.prnItemList(null);
						System.out.println("검색할 아이템 선택");
						item = inputItemInfo(false);//
						try {
							item = dao.searchItem(item);
							System.out.println(item);
						} catch (ArrayIndexOutOfBoundsException e) {
							System.out.println("해당 정보 없음");
						}
						break;
					case 5:
						System.out.println("목록");
						prnSubMenu();
						break;
					default:
						
						res=7;					
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}			
		} while (res<=6);
	}
	private void close(){
		CloserUtil.close(connection);
	}
	public abstract T inputItemInfo(boolean b);
	public abstract void prnSubMenu();
}
